package de.tum.wi.som.rcpsp.entities;

import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;

/**
 * Contains a set of activites with optimal order regarding resources and time
 * consumption.
 * 
 * 
 */
public class Schedule {
	private List<TimePeriod> timePeriodSchedule = new ArrayList<TimePeriod>();
	private WorkContentResource workContentResource;
	private int lowerBoundProjectEnd = 0; 

	public WorkContentResource getWorkContentResource() {
		return workContentResource;
	}

	public TimePeriod findOrCreateTimePeriod(int time) {
		TimePeriod timePeriod = findTimePeriod(time);
		if (timePeriod == null) {
			timePeriod = new TimePeriod(time);
			timePeriod.setSchedule(this);
			timePeriodSchedule.add(timePeriod);
		}
		return timePeriod;
	}

	public TimePeriod findTimePeriod(int time) {
		TimePeriod timePeriod = null;
		for (TimePeriod timePeriod2 : timePeriodSchedule) {
			if (timePeriod2.getTime() == time) {
				timePeriod = timePeriod2;
			}
		}
		return timePeriod;
	}

	public boolean addTime(TimePeriod timePeriod) {
		return timePeriodSchedule.add(timePeriod);
	}

	public boolean removeTime(TimePeriod timePeriod) {
		return timePeriodSchedule.remove(timePeriod);
	}

	public List<TimePeriod> getTimePeriods() {
		return this.timePeriodSchedule;
	}

	public Schedule(WorkContentResource workContentResource) {
		this.workContentResource = workContentResource;
	}
	
	
	@Override
	public String toString() {
		String result="";
		Collections.sort(timePeriodSchedule, new Comparator<TimePeriod>() {

			@Override
			public int compare(TimePeriod o1, TimePeriod o2) {
				return new Integer(o1.getTime()).compareTo(o2.getTime());
			}
			
		});
		for (TimePeriod time : timePeriodSchedule) {
			result+= time.toString() + "---- Available:" + time.getAvailableResourcesPerTime() + "; Limit: " + this.getWorkContentResource().getLimit() + "; Aktivitaeten: " + time.getAllAcitivitiesAsString() + "\n";
		}
		return result;
	}

	public int getLowerBoundProjectEnd() {
		return lowerBoundProjectEnd;
	}

	public void setLowerBoundProjectEnd(int lowerBoundProjectEnd) {
		this.lowerBoundProjectEnd = lowerBoundProjectEnd;
	}

}
